﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ko2.Portal;

using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Outlook;

namespace Imap
{

    public class KoImap
    {

        #region Members
        public Ko2.Portal.Env _env = new Ko2.Portal.Env();
        Outlook.Application mailSession = null;
        NameSpace mailNamespace = null;
        #endregion

        private System.Exception Login()
        {
            System.Exception ret = null;
            try
            {
                mailSession = new Microsoft.Office.Interop.Outlook.Application();
                mailNamespace = mailSession.GetNamespace("MAPI");
                string FarmUserUID = _env.GetAppSetting("FarmUserUID");
                string FarmUserPwd = _env.GetAppSetting("FarmUserPwd");
                mailNamespace.Logon(FarmUserUID, FarmUserPwd, false, true);
            }
            catch (System.Exception err)
            {
                ret = new System.Exception(err.Message, err);
            }
            return ret;
        }

        public string SearchInBox(string filter = "[Subject] > 's' And [Subject] <'u'")
        {
            MAPIFolder inbox = getInbox();
            Outlook.Items items = inbox.Items;
            Outlook.MailItem mailItem = null;
            object folderItem;
            string subjectName = string.Empty;
            folderItem = items.Find(filter);
            while (folderItem != null)
            {
                mailItem = folderItem as Outlook.MailItem;
                if (mailItem != null)
                {
                    subjectName += "\n" + mailItem.Subject;
                }
                folderItem = items.FindNext();
            }
            subjectName = " The following e-mail messages were found: " +
                subjectName;
            return subjectName;
        }

        public MAPIFolder getInbox()
        {
            MAPIFolder ret = null;
            if (mailSession == null)
            {
                System.Exception err = Login();
                if (err != null)
                    return ret;
            }
            ret =
                mailNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
            return ret;
        }

        public MAPIFolder getKoInFolder()
        {
            MAPIFolder ret = null;
            if (mailSession == null)
            {
                System.Exception err = Login();
                if (err != null)
                    return ret;
            }
            foreach (var f in mailNamespace.Folders)
            {
                if (((MAPIFolder)f).Name == "in")
                {
                    foreach (var ff in ((MAPIFolder)f).Folders)
                    {
                        if (((MAPIFolder)ff).Name.ToLower() == "inbox" || ((MAPIFolder)ff).Name.ToLower() == "входящие")
                        {
                            return ((MAPIFolder)ff);
                        }
                    }                    
                }
            }
            return ret;
        }
    }

}


